package com.sunday.android.fangdaifu.bukong.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.sunday.common.event.EventBus;
import com.sunday.member.base.BaseFragment;

import butterknife.ButterKnife;

/**
 * Created by littlexs on 2017/4/29.
 */

public abstract class LazyFragment extends BaseFragment {
    /** Fragment当前状态是否可见 */
    protected boolean isVisible;
    protected Context mContext;
    protected Intent intent;
    protected boolean isFinish = false;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public void onResume() {
        super.onResume();
        isFinish = false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFinish = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);//反注册EventBus
        }
        ButterKnife.unbind(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }
    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }
    /**
     * 不可见
     */
    protected void onInvisible() {
    }
    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();
}
